<?php

class SearchController extends Controller
{
    public $layout = '//layouts/general';
    
    /**
	 * @return array action filters
	 */
	public function filters()
	{
		return array(
			'rights', // perform access control for CRUD operations
		);
	}
    
    // Action dùng d? thanh toán bàn riêng
    public function actionStadium()
    {
        if(Yii::app()->request->isAjaxRequest)
        {
            $stadiumId = $_POST['type'];
            // Lay danh sach san
            $stadiums = Stadiums::model()->findAllByAttributes(array('stadium_type_id' => $stadiumId,'visible' => 1));
            $arrStadiums = array();
            $strStadiums = "";
            foreach ($stadiums as $item) {
                $arrStadiums[]= $item->name ;
                $strStadiums .= "'" . $item->name ."',";
            }
            $strStadiums = substr($strStadiums,0,-1);
            $to = $_POST['to'];
            $from = $_POST['from'];
            switch ($_POST['by']) {
               case 'today':
                    $fromDate = date('Y-m-d') . ' ' . $from . ':00';
                    $toDate = date('Y-m-d') . ' ' . $to . ':00';                    
                    $results = Jqcalendar::model()->findAll(
                        'StartTime >= :from and EndTime <= :to and Stadium in (' . $strStadiums . ')',
                        array(
                            ':from' => $fromDate,
                            ':to' => $toDate
                        )
                    );
                    $arrResult = array();
                    foreach ($results as $item) {
                        $arrResult[] = $item->Stadium;
                    };
                    $arrResult = array_unique($arrResult);
                    $this->renderPartial('stadium',array('result' => array_diff($arrStadiums,$arrResult),'index' => 0));
                    break;
               case 'week':
                    for ($index = 0;$index < 7;$index++) {
                        $fromDate = date('Y-m-d',strtotime(date('Y-m-d') ." +$index day")) . ' ' . $from . ':00';
                        $toDate = date('Y-m-d',strtotime(date('Y-m-d') ." +$index day")) . ' ' . $to . ':00';                    
                        $results = Jqcalendar::model()->findAll(
                            'StartTime >= :from and EndTime <= :to and Stadium in (' . $strStadiums . ')',
                            array(
                                ':from' => $fromDate,
                                ':to' => $toDate
                            )
                        );
                        $arrResult = array();
                        foreach ($results as $item) {
                            $arrResult[] = $item->Stadium;
                        };
                        $arrResult = array_unique($arrResult);
                        $this->renderPartial('stadium',array('result' => array_diff($arrStadiums,$arrResult),'index' => $index));
                    }
                    break;                   
               default :  
                throw new CHttpException(404,'The requested page does not exist.');
            }
            Yii::app()->end();
        }
        else
        {
            throw new CHttpException(404,'The requested page does not exist.');
        }
    }
}